package com.bsj.travel.process.file.log;

import com.alibaba.fastjson.JSON;
import com.bsj.getFile.GetFileAction;
import com.bsj.getFile.def.ResultObj;
import com.bsj.getFile.motion.CheckData;
import com.bsj.travel.def.monitor.CloudLog;
import com.bsj.travel.util.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import redis.clients.bsj.JedisClusterBSJ;

/**
 * @author yinhao
 * @version 1.0
 * @description 云端日志数据
 * @date 2024/1/26
 */
@Slf4j
public class LogSaveQueue {
    private static GetFileAction getFileAction;

    public static void start(String name) {
        JedisClusterBSJ jedisCli = SpringUtil.getBean(JedisClusterBSJ.class);
        getFileAction = new GetFileAction(new CheckData() {
            @Override
            public ResultObj check(byte[] bytes) {
                ResultObj resultObj = new ResultObj();
                String data = new String(bytes);
                CloudLog cloudLog = JSON.parseObject(data, CloudLog.class);
                //设备号-产品key
                String id = cloudLog.getIdentify().replaceAll("&", "-");
                resultObj.setFlag(true);
                resultObj.setContext(bytes);
                resultObj.setId(id);
                return resultObj;
            }
        }, jedisCli);
        getFileAction.start(name);
    }

    public static void stop() {
        if (getFileAction != null) {
            getFileAction.stop();
        }
    }


    public static void push(byte[] bytes) {
        if (bytes != null) {
            getFileAction.pushData(bytes);
        }
    }
}
